﻿using System;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Client;
using GSSoftSL.Views.NghiepVu;
using GSSoftSL.Web;
using GSSoftSL.Web.Models;
using GSSoftSL.Web.Services;
using GSSoftSL.Views;
using GSSoftSL.Views.HocVienView;
using GalaSoft.MvvmLight.Messaging;

namespace GSSoftSL
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using GSSoftSL.LoginUI;

    /// <summary>
    /// <see cref="UserControl"/> class providing the main UI for the application.
    /// </summary>
    public partial class MainPage : UserControl
    {
        //private GSSoftContext _dc;
        /// <summary>
        /// Creates a new <see cref="MainPage"/> instance.
        /// </summary>
        public MainPage()
        {
            InitializeComponent();
            /*var context = new GSSoftContext();
            LoadOperation<Truong> lo = context.Load(context.GetTruongsQuery());
            
            lo.Completed += (sender, e) =>
                                {
                                    LoadOperation<Truong> op = sender as LoadOperation<Truong>;
                                };*/
            //myDataGrid.ItemsSource = lo.Entities;
            Messenger.Default.Register<Visibility>(this, SetNavigationVisible);
            Messenger.Default.Register<List<string>>(this, SetNavigationPaneVisible);
        }

        /// <summary>
        /// After the Frame navigates, ensure the <see cref="HyperlinkButton"/> representing the current page is selected
        /// </summary>
        private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            /*foreach (UIElement child in LinksStackPanel.Children)
            {
                HyperlinkButton hb = child as HyperlinkButton;
                if (hb != null && hb.NavigateUri != null)
                {
                    if (hb.NavigateUri.ToString().Equals(e.Uri.ToString()))
                    {
                        VisualStateManager.GoToState(hb, "ActiveLink", true);
                    }
                    else
                    {
                        VisualStateManager.GoToState(hb, "InactiveLink", true);
                    }
                }
            }*/
        }

        /// <summary>
        /// If an error occurs during navigation, show an error window
        /// </summary>
        private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            e.Handled = true;
            ErrorWindow.CreateNew(e.Exception);
        }

        private void btnTruong_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
			//MainPanel.Content = new TruongView();
            //MessageWindow.CreateNew("Trường");
            CloseCurrent();
            MainPanel.Children.Add(new TruongView());
        }
		
		private void btnPhong_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			//MainPanel.Content = new PhongView();
            CloseCurrent();
			MainPanel.Children.Add(new PhongView());
		}
		
		private void btnMon_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			//MainPanel.Content = new MonView();
            CloseCurrent();
            MainPanel.Children.Add(new MonView());
		}
		
		private void btnGiaoVien_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			//MainPanel.Content = new GiaoVienView();
            CloseCurrent();
            MainPanel.Children.Add(new GiaoVienView());
		}
		
		private void btnLop_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            //MessageWindow.CreateNew("Lớp");
			//MainPanel.Content = new LopView();
            CloseCurrent();
            MainPanel.Children.Add(new LopView());
		}
		
		private void btnCaHoc_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	CloseCurrent();
            MainPanel.Children.Add(new CaHocView());
        }
		
		private void btnKhoaHoc_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			//MainPanel.Content = new KhoaHocView();
            CloseCurrent();
            MainPanel.Children.Add(new KhoaHocView());
		}
		
		private void btnHocVien_Click(object sender, RoutedEventArgs e)
	    {
            //MainPanel.Content = new HocVienMainView();
            CloseCurrent();
            MainPanel.Children.Add(new HocVienMainView());
	    }
		
        private void SetNavigationVisible(Visibility status)
        {
            outlookBar1.Visibility = status;
            if (status == Visibility.Collapsed)
                MainPanel.Children.Clear();
        }

        private void SetNavigationPaneVisible(List<string> ListQuyen)
        {
            paneBaoBieu.Visibility = ListQuyen.Contains(EnumRole.BaoBieu) ? Visibility.Visible : Visibility.Collapsed;
            paneDanhMuc.Visibility = ListQuyen.Contains(EnumRole.DanhMuc) ? Visibility.Visible : Visibility.Collapsed;
            paneNghiepVu.Visibility = ListQuyen.Contains(EnumRole.NghiepVu) ? Visibility.Visible : Visibility.Collapsed;
        }

        private void btnReport_Click(object sender, RoutedEventArgs e)
        {
            ChildWindow child = new ChildWindow();
            child.Content = new ReportView();
            child.Height = 500;
            child.Width = 600;
            child.Show();
        }

        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            /*if (WebContext.Current.User.IsAuthenticated)
                outlookBar1.Visibility = Visibility.Visible;
            else
                outlookBar1.Visibility = Visibility.Collapsed;*/
        }

        private void btnDiemDanh_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            CloseCurrent();
            MainPanel.Children.Add(new DiemDanhView());
        }

        private void btnDiemSo_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            CloseCurrent();
            MainPanel.Children.Add(new DiemSoMainView());
        }

        private void CloseCurrent()
        {
            MainPanel.Children.Clear();
        }

        
    }
}